package d1208.Ljs2.Ljs26;

import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Properties;

public class Demo1 {
    //现有User类和data.properties 文件，要求读取data.properties 文件，
    //通过反射创建出User类的对象,调用set方法将属性赋值，使用反射调用toString方法 打印对象。
    public static void main(String[] args) throws Exception{
        FileInputStream fileInputStream = new FileInputStream("E:\\代码\\代码练习和作业\\Zuoye12月6号\\src\\Ljs2\\Ljs14\\t1.properties");
        Properties properties = new Properties();
        properties.load(fileInputStream);
        fileInputStream.close();
        System.out.println(properties);
        Class<?> c = Class.forName("Ljs2.Ljs26.User");
        Constructor<?> constructor = c.getConstructor();
        User user=(User) constructor.newInstance();
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            if (entry.getKey().equals("uid")){
                Field uid = c.getDeclaredField("uid");
                uid.setAccessible(true);
                uid.set(user,entry.getValue());
                System.out.println(uid.get(user));
            }
            if (entry.getKey().equals("username")){
                Field username = c.getDeclaredField("username");
                username.setAccessible(true);
                username.set(user,entry.getValue());
                System.out.println(username.get(user));
            }
            if (entry.getKey().equals("password")){
                Field password = c.getDeclaredField("password");
                password.setAccessible(true);
                password.set(user,entry.getValue());
                System.out.println(password.get(user));
            }
        }
    }

}
